package pl.veneo.wigprofit.view {
	import flash.events.Event;
	import pl.veneo.wigprofit.view.elements.LegendElement;
	import pl.veneo.wigprofit.ctrl.events.ChartResizeEvent;
	import pl.veneo.wigprofit.ctrl.events.SliderUpdateEvent;
	import pl.veneo.wigprofit.model.PeriodData;
	import pl.veneo.wigprofit.view.elements.ChartArrow;
	import pl.veneo.wigprofit.view.elements.ChartLines;
	import pl.veneo.wigprofit.view.elements.ChartScales;
	import pl.veneo.wigprofit.view.elements.SliderBar;

	import flash.display.Sprite;
	import flash.events.EventDispatcher;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartView extends EventDispatcher {
		
		private var container : Sprite;
		private var _currentPeriod : PeriodData;
		
		private var _chartBackground : Sprite;
		private var _chartLines : ChartLines;
		private var _chartScales : ChartScales;
		private var _chartArrow : ChartArrow;
		private var _slider : SliderBar;
		private var _legend : LegendElement;

		public function ChartView(container : Sprite) {
			
			super();
			
			this.container = container;
			
			_chartBackground = container.addChild(new Sprite()) as Sprite;
			_chartBackground.graphics.beginFill(0xFFFFFF,1);
			_chartBackground.graphics.drawRect(0, 0, maxWidth - 100, maxHeigth - 70);
			_chartBackground.graphics.endFill();
			_chartBackground.x = (600 - _chartBackground.width) /2;
			_chartBackground.y = - 20 + (400 - _chartBackground.height) /2;
			//_chartBackground.visible = false;
			
			_chartScales = container.addChild(new ChartScales(_chartBackground.width, _chartBackground.height)) as ChartScales;
			_chartScales.x = _chartBackground.x;
			_chartScales.y = _chartBackground.y;
					
			_chartLines = container.addChild(new ChartLines(_chartBackground.width, _chartBackground.height)) as ChartLines;
			_chartLines.x = _chartBackground.x;
			_chartLines.y = _chartBackground.y;
			_chartLines.mouseEnabled = false;
			_chartLines.mouseChildren = false;
			
			_chartArrow = container.addChild(new ChartArrow(_chartBackground.width, _chartBackground.height)) as ChartArrow;
			_chartArrow.x = _chartBackground.x;
			_chartArrow.y = _chartBackground.y;
			_chartArrow.addEventListener(ChartResizeEvent.ON_CHART_RESIZE, onForceChartUpdate);
			
			_legend = container.addChild(new LegendElement()) as LegendElement;
			_legend.x = _chartBackground.x + 7;
			_legend.y = _chartBackground.y + 7;
			_legend.addEventListener(LegendElement.TOGGLE_CHART, onChartToggle);
			
			
			_slider = container.addChild(new SliderBar(_chartBackground.width, _chartBackground.height)) as SliderBar;
			_slider.x = _chartBackground.x;
			_slider.y = _chartBackground.y + _chartBackground.height + 10;
			_slider.addEventListener(SliderUpdateEvent.ON_SLIDER_CHANGE, onSliderUpdate);
			_slider.addEventListener(ChartResizeEvent.ON_CHART_RESIZE, onForceChartUpdate);
			
		}

		private function onChartToggle(event : Event) : void {
			
			_chartLines.showChart1(_legend.toggle1);
			_chartLines.showChart2(_legend.toggle2);
			
		}

		private function onForceChartUpdate(event : ChartResizeEvent) : void {
			
			trace(event.start+" "+event.end);
			dispatchEvent(new ChartResizeEvent(ChartResizeEvent.ON_CHART_RESIZE, event.start, event.end, event.force));
			
		}

		private function onSliderUpdate(event : SliderUpdateEvent) : void {
			
			_chartLines.resizeView(event.sliderX, event.sliderWidth);
			
		}

		public function updateChartForPeriod(period : PeriodData) : void {
			
			_currentPeriod = period;
			//_chartScales.draw(period);
			_chartArrow.draw(period);
			
		}

		public function buildChartForPeriod(period : PeriodData) : void {
			
			_currentPeriod = period;
			_chartLines.draw(period);
			_chartScales.draw(period);
			_chartArrow.draw(period);
			_slider.init(period);
			
		}
		
		private function get maxWidth() : Number {
			
			return container.stage.stageWidth;
		
		}
		
		private function get maxHeigth() : Number {
		
			return container.stage.stageHeight;
		
		}
	}
}
